401b14
@@ -124,6 +124,7 @@
   private static final ResourceBundle resourceBundle =
       ResourceBundle.getBundle(BeeLine.class.getSimpleName());
   private final BeeLineSignalHandler signalHandler;
+  private final Runnable shutdownHook;
   private static final String separator = System.getProperty("line.separator");
   private boolean exit = false;
   private final DatabaseConnections connections = new DatabaseConnections();
@@ -532,13 +533,26 @@
public BeeLine() {
   public BeeLine(boolean isBeeLine) {
     this.isBeeLine = isBeeLine;
     this.signalHandler = new SunSignalHandler(this);
+    this.shutdownHook = new Runnable() {
+      @Override
+      public void run() {
+        try {
+          if (history != null) {
+            history.flush();
+          }
+        } catch (IOException e) {
+          error(e);
+        } finally {
+          close();
+        }
+      }
+    };
   }
 
   DatabaseConnection getDatabaseConnection() {
     return getDatabaseConnections().current();
   }
 
-
   Connection getConnection() throws SQLException {
     if (getDatabaseConnections().current() == null
         || getDatabaseConnections().current().getConnection() == null) {
@@ -985,6 +999,9 @@
public int begin(String[] args, InputStream inputStream) throws IOException {
 
     setupHistory();
 
+    //add shutdown hook to cleanup the beeline for smooth exit
+    addBeelineShutdownHook();
+
     //this method also initializes the consoleReader which is
     //needed by initArgs for certain execution paths
     ConsoleReader reader = initializeConsoleReader(inputStream);
@@ -1184,17 +1201,11 @@
private void setupHistory() throws IOException {
     }
 
     this.history = new FileHistory(new File(getOpts().getHistoryFile()));
-    // add shutdown hook to flush the history to history file
-    ShutdownHookManager.addShutdownHook(new Runnable() {
-      @Override
-      public void run() {
-        try {
-          history.flush();
-        } catch (IOException e) {
-          error(e);
-        }
-      }
-    });
+  }
+
+  private void addBeelineShutdownHook() throws IOException {
+    // add shutdown hook to flush the history to history file and it also close all open connections
+    ShutdownHookManager.addShutdownHook(getShutdownHook());
   }
 
   public ConsoleReader initializeConsoleReader(InputStream inputStream) throws IOException {
@@ -2272,6 +2283,10 @@
DatabaseConnections getDatabaseConnections() {
     return connections;
   }
 
+  Runnable getShutdownHook() {
+    return shutdownHook;
+  }
+
   Completer getCommandCompletor() {
     return beeLineCommandCompleter;
   }
